package com.util.regex;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternDemo {
    /**
     * 查询是否是电信号码
     */
    @Test
    public void test01(){
        String code_number = null;
        Pattern p= Pattern.compile("^1(33|53|80|81|89|77|73|99)(\\d){8}$");
        if(code_number ==null || !p.matcher(code_number).find()){
            // 验证码验证返回
            System.out.println("no");
            return;
        }
        System.out.println("true");

    }
    @Test
    public  void  test(){
        //判断字符
        Pattern p = Pattern.compile("\"SG:([a-zA-Z0-9]+):orderSn:([a-zA-Z0-9]+)\"");
        //\\w 会匹配_
        //        Pattern p = Pattern.compile("SG:(\\w+):orderSn:(\\w+)");
        String s = "\"SG:SG76190207124503X004:orderSn:TYJD19020712450343532\"";
        Matcher m = p.matcher(s);
        if (m.matches()) {//这句必须有，要不然后面无法从group里面取值
            System.out.println(m.groupCount());
            for (int i = 0; i <= m.groupCount(); i++) {
                System.out.println(m.group(i));
            }
            System.out.println();
        }
    }

    @Test
    public void test02(){

        String reg = "【浙江电信】您的手机验证码为：([0-9]{4,6})，([0-9]{1,4})分钟内有效，请确保本人操作。";

        String s = "【浙江电信】您的手机验证码为：013841，30分钟内有效，请确保本人操作。";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(s);
        if(m.matches()){
            System.out.println(m.groupCount());
            for(int i = 0 ;i<=m.groupCount();i++){
                System.out.println(m.group(i));
            }
        }


//        Pattern p = Pattern.compile("【浙江电信】您的手机验证码为：([0-9]{4,6})，([0-9]{1,4})分钟内有效，请确保本人操作。");
//        //\\w 会匹配_
//        //        Pattern p = Pattern.compile("SG:(\\w+):orderSn:(\\w+)");
//        String s = "【浙江电信】您的手机验证码为：013841，30分钟内有效，请确保本人操作。";
//        Matcher m = p.matcher(s);
//        if (m.matches()) {//这句必须有，要不然后面无法从group里面取值
//            System.out.println(m.groupCount());
//            for (int i = 0; i <= m.groupCount(); i++) {
//                System.out.println(m.group(i));
//            }
//            System.out.println();
//        }
    }


    /**
     * url正则匹配
     */
    @Test
    public void test03(){
        String reg = "https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";

        String s = "http://wapzj.189.cn/mall/mo/?opor=%252B%252B76BsvgMM8WesuBWUcfNg%253D%";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(s);
        System.out.println(m.matches());
        if(m.matches()){
            System.out.println(m.groupCount());
            for(int i = 0 ;i<=m.groupCount();i++){
                System.out.println(m.group(i));
            }
        }
    }


    @Test
    public void test04(){
        String reg = "^([^\\*]+)([^\\*]+).+锁单$";

        String s = "徐尧*新装*温州*吴义敏 33038119860918221X 温州瑞安云周街道上步村  13655878563锁单";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(s);
        System.out.println(m.matches());
        if(m.matches()){
            System.out.println(m.groupCount());
            for(int i = 0 ;i<=m.groupCount();i++){
                System.out.println(m.group(i));
            }
        }
    }

    /**
     * 日期时间段划分
     * 2019-10-15 19:00:00-2019-12-31 00:00:00
     * 2019-10-15 00:00:00-永久
     */
    @Test
    public void test05() throws ParseException {
        Date date = new Date();

//        String code_number = "2019-10-15 19:00:00-2019-12-31 00:00:00";
        String code_number = "2019-10-15 00:00:00-永久";
        Pattern p= Pattern.compile("^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})(-)(.*)$");
        Matcher m = p.matcher(code_number);

        if(m.matches()){
            if(m.groupCount()!=3){
                System.out.println("天猫旗舰店时间配置错误");
            }
            else{
                String start = m.group(1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date startDate  = sdf.parse(start);

                if("永久".equals(m.group(3))){
                    if(startDate.getTime()<date.getTime()){
//                        return true;
                    }
                }else{
                    String end = m.group(3);
                    Date endDate  = sdf.parse(end);
                    if(startDate.getTime()<date.getTime()&& date.getTime()<endDate.getTime()){
//                        return true;
                    }
                }
            }
        }


        System.out.println("true");

    }

/*
    匹配短信验证码
    String reg = "【浙江电信】您的手机验证码为：([0-9]{4,6})，([0-9]{1,4})分钟内有效，请确保本人操作。";

    String s = "【浙江电信】您的手机验证码为：013841，30分钟内有效，请确保本人操作。";

* */

}
